﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;


namespace myWindowsMediaPlayer.Model
{
    public sealed class customSection : ConfigurationSection
    {
        public customSection()
        {
        }

        [ConfigurationProperty("Name",
            DefaultValue = "myWindowsMediaPlayer",
            IsRequired = true,
            IsKey = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("moviePath",
            DefaultValue = "",
            IsRequired = true,
            IsKey = true)]
        public string MoviePath
        {
            get { return (string)this["moviePath"]; }
            set { this["moviePath"] += value + ";"; }
        }

        [ConfigurationProperty("picturePath",
           DefaultValue = "",
           IsRequired = true,
           IsKey = true)]
        public string PicturePath
        {
            get { return (string)this["picturePath"]; }
            set { this["picturePath"] += value + ";"; }
        }

        [ConfigurationProperty("musicPath",
         DefaultValue = "",
         IsRequired = true,
         IsKey = true)]
        public string MusicPath
        {
            get { return (string)this["musicPath"]; }
            set { this["musicPath"] += value + ";"; }
        }
    }

    class myConfigManager
    {
        private Configuration config;        
            
        public myConfigManager()
        {
            customSection section = null;

            this.config = ConfigurationManager.OpenExeConfiguration("myWindowsMediaPlayer.config") as Configuration;

            section = config.GetSection("customSection") as customSection;

            if (section == null)
            {
                section = new customSection();

                config.Sections.Add("customSection", section);
                section.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Full);
            }
        }

        public string MusicPath
        {
            get 
            {
                customSection section = config.Sections["customSection"] as customSection;
                return (section != null ? section.MusicPath : null);
            }
            set
            {
                customSection section = this.config.Sections["customSection"] as customSection;

                if (section == null)
                    config.Sections.Add("customSection", section);
                section.MusicPath = value;
                section.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Modified);
            }
        }

        public string PicturePath
        {
            get
            {
                customSection section = config.Sections["customSection"] as customSection;
                return (section != null ? section.PicturePath : null);
            }
            set
            {
                customSection section = this.config.Sections["customSection"] as customSection;

                if (section == null)
                    config.Sections.Add("customSection", section);
                section.PicturePath = value;
                section.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Modified);
            }
        }

        public string MoviePath
        {
            get 
            {
                customSection section = config.Sections["customSection"] as customSection;
                return (section != null ? section.MoviePath : null);
            }
            set
            {
                customSection section = this.config.Sections["customSection"] as customSection;

                //if (section == null)
                //    config.Sections.Add("customSection", section);
                //section.MoviePath = value;

                //section.SectionInformation.ForceSave = true;
                //config.Save(ConfigurationSaveMode.Modified);
                section.MoviePath = value;
                config.AppSettings.Settings.Add("MoviePath", section.MoviePath);
                config.Save(ConfigurationSaveMode.Modified);
            }
        }
    }
}
